//
//  ViewController.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 12-12-6.
//  Copyright (c) 2012年 wzyk. All rights reserved.
//

#import "ViewController.h"
#import "ABTabButton.h"
#import "ChannelSelectView.h"

@interface ViewController ()

@end

@implementation ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showChannel:) name:kShowChannel object:nil];
    }
    return self;
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if ([request.URL.absoluteString rangeOfString:@"tab.html"].length) {
        [animateWebView removeFromSuperview];
        return NO;
    }
    return YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
//    NSURL *url = [[NSBundle mainBundle] URLForResource:@"app/zhong.html" withExtension:nil];
//    [animateWebView loadRequest:[NSURLRequest requestWithURL:url]];
    
    [self initTabView];
    
    homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
    channelViewController = [[ChannelViewController alloc] initWithNibName:@"ChannelViewController" bundle:nil];
    shelfViewController = [[ShelfViewController alloc] initWithNibName:@"ShelfViewController" bundle:nil];
    accountViewController = [[AccountViewController alloc] initWithNibName:@"AccountViewController" bundle:nil];
    settingViewController = [[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil];
    
    [self selectTabButton:kTabButtonHome];
}

- (void)initTabView
{
    ABTabButton *homeButton = [[[ABTabButton alloc] initWithImage:[UIImage imageNamed:@"tab_index"] highlightedImage:[UIImage imageNamed:@"tab_index_selected"] title:@"首页"] autorelease];
    homeButton.frame = CGRectMake(0, 4, TabButtonWidth, TabButtonHeigth);
    [homeButton addTarget:self action:@selector(tabButtonAction:)];
    homeButton.tag = kTabButtonHome;
    [tabView addSubview:homeButton];
    
    ABTabButton *channelButton = [[[ABTabButton alloc] initWithImage:[UIImage imageNamed:@"tab_channel"] highlightedImage:[UIImage imageNamed:@"tab_channel_selected"] title:@"频道"] autorelease];
    channelButton.frame = CGRectMake(TabButtonWidth, 4, TabButtonWidth, TabButtonHeigth);
    [channelButton addTarget:self action:@selector(tabButtonAction:)];
    channelButton.tag = kTabButtonChannel;
    [tabView addSubview:channelButton];
    
    ABTabButton *shelfButton = [[[ABTabButton alloc] initWithImage:[UIImage imageNamed:@"tab_bookshelf"] highlightedImage:[UIImage imageNamed:@"tab_bookshelf_selected"] title:@"书架"] autorelease];
    shelfButton.frame = CGRectMake(TabButtonWidth*2, 4, TabButtonWidth, TabButtonHeigth);
    [shelfButton addTarget:self action:@selector(tabButtonAction:)];
    shelfButton.tag = kTabButtonShelf;
    [tabView addSubview:shelfButton];
    
    ABTabButton *accountButton = [[[ABTabButton alloc] initWithImage:[UIImage imageNamed:@"tab_account"] highlightedImage:[UIImage imageNamed:@"tab_account_selected"] title:@"账号"] autorelease];
    accountButton.frame = CGRectMake(TabButtonWidth*3, 4, TabButtonWidth, TabButtonHeigth);
    [accountButton addTarget:self action:@selector(tabButtonAction:)];
    accountButton.tag = kTabButtonAccount;
    [tabView addSubview:accountButton];
    
    ABTabButton *settingButton = [[[ABTabButton alloc] initWithImage:[UIImage imageNamed:@"tab_setting"] highlightedImage:[UIImage imageNamed:@"tab_setting_selected"] title:@"设置"] autorelease];
    settingButton.frame = CGRectMake(TabButtonWidth*4, 4, TabButtonWidth, TabButtonHeigth);
    [settingButton addTarget:self action:@selector(tabButtonAction:)];
    settingButton.tag = kTabButtonSetting;
    [tabView addSubview:settingButton];
}

- (void)tabButtonAction:(UIView *)sender
{
    [self selectTabButton:sender.tag];
}

- (void)selectTabButton:(NSInteger)tag
{
    if (currentTab == tag && tag != kTabButtonChannel) {//频道按钮可重复点击
        return;
    }
    
    for (ABTabButton *button in tabView.subviews) {
        if ([button isKindOfClass:[ABTabButton class]]) {
            [button setSelected:(button.tag == tag)];
        }
    }
    
    switch (tag) {
        case kTabButtonHome:
            [homeViewController updateDisplay];
            [self clearContentView];
            homeViewController.view.frame = contentView.bounds;
            [contentView addSubview:homeViewController.view];
            break;
        case kTabButtonChannel:
        {
//            ChannelSelectView *channelSelectView = [[[NSBundle mainBundle] loadNibNamed:@"ChannelSelectView" owner:self options:nil] objectAtIndex:0];
//            channelSelectView.frame = self.view.bounds;
//            [self.view addSubview:channelSelectView];
            
//            [channelViewController updateDiaplay];
            [self clearContentView];
            channelViewController.view.frame = contentView.bounds;
            [contentView addSubview:channelViewController.view];
        }
            break;
        case kTabButtonShelf:
            [shelfViewController updateDiaplay];
            [self clearContentView];
            shelfViewController.view.frame = contentView.bounds;
            [contentView addSubview:shelfViewController.view];
            break;
        case kTabButtonAccount:
            [accountViewController updateDisplay];
            [self clearContentView];
            accountViewController.view.frame = contentView.bounds;
            [contentView addSubview:accountViewController.view];
            break;
        case kTabButtonSetting:
            [settingViewController updateDiaplay];
            [self clearContentView];
            settingViewController.view.frame = contentView.bounds;
            [contentView addSubview:settingViewController.view];
            break;
        default:
            break;
    }
    currentTab = tag;
}

- (void)clearContentView
{
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
}

//- (void)showChannel:(NSNotification *)notification
//{
//    if (![notification.object isKindOfClass:[NSDictionary class]]) {
//        return;
//    }
//    if (channelViewController.view.superview == nil) {
//        [self clearContentView];
//        channelViewController.view.frame = contentView.bounds;
//        [contentView addSubview:channelViewController.view];
//    }
//    [channelViewController updateDisplayWithChannel:notification.object];
//}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [tabView release];
    [contentView release];
    [animateWebView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [tabView release];
    tabView = nil;
    [contentView release];
    contentView = nil;
    [animateWebView release];
    animateWebView = nil;
    [super viewDidUnload];
}
@end
